package com.zy.imagineCommunity.service;

import com.zy.imagineCommunity.dao.SystemMessageMapper;
import com.zy.imagineCommunity.entity.Page;
import com.zy.imagineCommunity.entity.SystemMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemMessageService {

    @Autowired
    SystemMessageMapper systemMessageMapper;

    /**
     * 根据页码、每页个数查询系统通知
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<SystemMessage> querySystemMessageByPageNoAndPageSize(int pageNo, int pageSize) {
        Page<SystemMessage> page = new Page<SystemMessage>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = systemMessageMapper.querySystemMessageTotalCount();
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<SystemMessage> SystemMessageList = systemMessageMapper.querySystemMessageListByBeginAndPageSize(begin, pageSize);
        //设置每页内容
        page.setItems(SystemMessageList);
        return page;
    }

    /**
     * 增加公告
     *
     * @param systemMessage
     */
    public void addSystemMessage(SystemMessage systemMessage) {
        systemMessageMapper.addSystemMessage(systemMessage);
    }

    /**
     * 查询单个公告
     *
     * @param id
     * @return
     */
    public SystemMessage querySystemMessageById(int id) {
        return systemMessageMapper.querySystemMessageById(id);
    }

    /**
     * 通过id删除公告
     *
     * @param id
     */
    public void deleteSystemMessage(int id) {
        systemMessageMapper.deleteSystemMessageById(id);
    }

    /**
     * @param pageNo
     * @param pageSize
     * @param searchValue
     * @return
     */
    public Page<SystemMessage> searchSystemMessageByPageNoAndPageSizeAndSearchValue(int pageNo, int pageSize, String searchValue) {
        Page<SystemMessage> page = new Page<SystemMessage>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = systemMessageMapper.querySystemMessageTotalCountAndSearchValue(searchValue);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<SystemMessage> SystemMessageList = systemMessageMapper.querySystemMessageListByBeginAndPageSizeAndSearchValue(begin, pageSize, searchValue);
        //设置每页内容
        page.setItems(SystemMessageList);
        return page;
    }

}
